<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>This tests that querySelector, querySelectorAll and matchesSelector (webkitMatchesSelector) don't crash when used in a viewless document.</title>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
</head>
<body>
<script>
setup({ single_test: true });

var testDoc = document.implementation.createDocument("http://www.w3.org/1999/xhtml", "html");
testDoc.documentElement.appendChild(testDoc.createElement("body"));
testDoc.body.appendChild(testDoc.createElement("p")).id = "p1";
testDoc.getElementById("p1").appendChild(testDoc.createElement("span")).id = "s1";
testDoc.body.appendChild(testDoc.createElement("span")).id = "s2";
testDoc.body.appendChild(testDoc.createElement("div")).className = "d1";

var p1 = testDoc.getElementById("p1");
var s1 = testDoc.getElementById("s1");
var s2 = testDoc.getElementById("s2");
var d1 = testDoc.body.lastChild;

assert_equals(testDoc.querySelector('p'), p1);
assert_equals(testDoc.querySelectorAll('span').length, 2);
assert_equals(testDoc.querySelectorAll('span').item(1), s2);
assert_equals(testDoc.querySelector('.d1'), d1);
assert_equals(testDoc.querySelectorAll('p span').length, 1);

assert_true(p1.webkitMatchesSelector('p'));
assert_true(s1.webkitMatchesSelector('p span'));
assert_true(s2.webkitMatchesSelector('#s2'));
assert_true(d1.webkitMatchesSelector('.d1'));
done();
</script>
</body>
</html>
